package orlowski.kamil.simple.app.activity;


import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import orlowski.kamil.simple.app.MainApplication;
import orlowski.kamil.simple.app.R;
import orlowski.kamil.simple.app.adapter.CountryListAdapter;
import orlowski.kamil.simple.app.domain.Country;
import orlowski.kamil.simple.app.service.CountryService;
import orlowski.kamil.simple.app.service.ICountryService;

import java.util.List;

/**
 * Created by Kamil on 2014-10-15.
 */
public class MenuListActivity extends ListActivity{
    private List<Country> countryList;
    private MainApplication mainApplication;
    private CountryListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_list);

        mainApplication = (MainApplication) getApplication();

        ICountryService service = new CountryService();
        countryList = service.getAllCountries(this);
        this.adapter = new CountryListAdapter(this, android.R.layout.simple_list_item_1, countryList);
        setListAdapter(adapter);
        setSearchListener(adapter);
    }

    private void setSearchListener(final CountryListAdapter adapter) {
        final EditText searchInput = (EditText) findViewById(R.id.search);
        searchInput.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                System.out.println("Text [" + s + "]");
                adapter.getFilter().filter(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
        });
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Country country = (Country) getListView().getItemAtPosition(position);
        mainApplication.setSelectedCountry(country);
        Intent detail = new Intent(MenuListActivity.this, DetailsActivity.class);
        startActivity(detail);
    }

    public List<Country> getCountryList() {
        return countryList;
    }

    public void setCountryList(List<Country> countryList) {
        this.countryList = countryList;
    }

    public MainApplication getMainApplication() {
        return mainApplication;
    }

    public void setMainApplication(MainApplication mainApplication) {
        this.mainApplication = mainApplication;
    }

    public CountryListAdapter getAdapter() {
        return adapter;
    }

    public void setAdapter(CountryListAdapter adapter) {
        this.adapter = adapter;
    }
}